<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="app">
        <div>
            <button @click="comName = 'demo'" :disabled="comName === 'demo'" :style="{background: (comName==='demo'?'pink':'')}">切换到demo组件</button>
            <button @click="comName = 'test'" :disabled="comName === 'test'" :style="{background: (comName==='test'?'pink':'')}">切换到test组件</button>
        </div>
        <keep-alive>
            <component :is="comName"></component>
        </keep-alive>
    </div>
    
    <template id="demo">
        <div>
            <h3>我是demo动态组件---{{msg}}</h3>
        </div>
    </template>

    <template id="test">
        <div>
            <h3>我是test动态组件---{{msg}}</h3>
        </div>
    </template>
</body>
<script src="./vue.js"></script>
<script>
    Vue.component('demo',{
        template:'#demo',
        data() {
            return {
                msg: 'Hello World'
            }
        },
        activated(){
            console.log('demo组件激活了');
        },
        deactivated(){
            console.log('demo组件失效了');
        },
    });
    Vue.component('test',{
        template:'#test',
        data() {
            return {
                msg: '你好呀！世界'
            }
        },
        activated(){
            console.log('test组件激活了');
        },
        deactivated(){
            console.log('test组件失效了');
        }
    });
    new Vue({
        el:'#app',
        data() {
            return {
                comName: 'demo' 
            }
        },
    })
</script>
</html>